// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: GPL-3.0-only

package badger_dao_impl

import (
	"context"
	"path/filepath"

	"gitcode.com/openseaotter/so_proto_gen_go.git/watch_api"
	"gitcode.com/openseaotter/so_server/storage/core"
	"gitcode.com/openseaotter/so_server/utils"
	"github.com/dgraph-io/badger/v4"
	"google.golang.org/protobuf/proto"
)

type WatchInfoList []*watch_api.WatchInfo

func (a WatchInfoList) Len() int           { return len(a) }
func (a WatchInfoList) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a WatchInfoList) Less(i, j int) bool { return a[i].CreateTime < a[j].CreateTime }

type _WatchInfoDao struct {
	dbh *badger.DB
}

func NewWatchInfoDao() (core.WatchInfoDao, error) {
	dbPath := filepath.Join(utils.DATA_DIR_PATH, "watch")
	options := badger.DefaultOptions(dbPath)
	dbh, err := badger.Open(options)
	if err != nil {
		return nil, err
	}
	return &_WatchInfoDao{dbh: dbh}, nil
}

func (db *_WatchInfoDao) Insert(ctx context.Context, info *watch_api.WatchInfo) error {
	return db.Update(ctx, info)
}

func (db *_WatchInfoDao) Exist(ctx context.Context, watchId string) (bool, error) {
	txn := db.dbh.NewTransaction(false)
	defer txn.Discard()

	item, err := txn.Get([]byte(watchId))
	if err != nil {
		if err == badger.ErrKeyNotFound {
			return false, nil
		}
		return false, err
	}
	if item.IsDeletedOrExpired() {
		return false, nil
	}
	return true, nil
}
func (db *_WatchInfoDao) Get(ctx context.Context, watchId string) (*watch_api.WatchInfo, error) {
	txn := db.dbh.NewTransaction(false)
	defer txn.Discard()

	item, err := txn.Get([]byte(watchId))
	if err != nil {
		return nil, err
	}
	if item.IsDeletedOrExpired() {
		return nil, badger.ErrKeyNotFound
	}
	value, err := item.ValueCopy(nil)
	if err != nil {
		return nil, err
	}
	info := &watch_api.WatchInfo{}
	err = proto.Unmarshal(value, info)
	if err != nil {
		return nil, err
	}
	return info, nil
}

func (db *_WatchInfoDao) Update(ctx context.Context, info *watch_api.WatchInfo) error {
	txn := db.dbh.NewTransaction(true)
	defer txn.Discard()

	value, err := proto.Marshal(info)
	if err != nil {
		return err
	}
	err = txn.Set([]byte(info.WatchId), value)
	if err != nil {
		return err
	}
	return txn.Commit()
}

func (db *_WatchInfoDao) Remove(ctx context.Context, watchId string) error {
	txn := db.dbh.NewTransaction(true)
	defer txn.Discard()

	err := txn.Delete([]byte(watchId))
	if err != nil {
		return err
	}
	return txn.Commit()
}
func (db *_WatchInfoDao) List(ctx context.Context) ([]*watch_api.WatchInfo, error) {
	txn := db.dbh.NewTransaction(false)
	defer txn.Discard()

	iter := txn.NewIterator(badger.DefaultIteratorOptions)
	defer iter.Close()

	watchList := []*watch_api.WatchInfo{}
	for iter.Rewind(); iter.Valid(); iter.Next() {
		item := iter.Item()
		if item.IsDeletedOrExpired() {
			continue
		}
		value, err := item.ValueCopy(nil)
		if err != nil {
			return nil, err
		}
		info := &watch_api.WatchInfo{}
		err = proto.Unmarshal(value, info)
		if err != nil {
			return nil, err
		}
		watchList = append(watchList, info)
	}
	return watchList, nil
}
